<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\Common;
use app\common\model\PlanStudentRel as PlanStudentRelModel;
use app\common\model\ViewGetStudentUnrelList as ViewGetStudentUnrelListModel;
use app\common\model\Config as ConfigModel;
use app\common\model\BookCommon as BookCommonasModel;
use app\common\model\RxndCache as RxndCacheasModel;
use app\common\model\ViewGetRel as ViewGetRelasModel;
use app\common\model\ViewGetSpecicalties as ViewGetSpecicaltiesModel;
use think\facade\Request;
use think\Db;
use think\facade\Log;

class PlanStudentRel extends Manage
{

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $planStudentRelModel = new PlanStudentRelModel();
            return $planStudentRelModel->tableData(input('param.'));
        }
        
        $bookCommon = new BookCommonasModel();
        $yearList = $bookCommon->getYears();  // 获取年度列表
        $seasonList = $bookCommon->getSeasons();  // 获取季度列表
        $deptList = $bookCommon->getDepts();  // 获取部门列表
        $dirList = $bookCommon->getDirs();  // 获取方向列表
        $this->assign('yearList', $yearList);
        $this->assign('seasonList', $seasonList);
        $this->assign('deptList', $deptList);
        $this->assign('dirList', $dirList);

        // 默认值
        $configModel = new ConfigModel();
        $year = $configModel->getYear();
        $season = $configModel->getSeason();
        $this->assign('year', $year);
        $this->assign('season', $season);
        
        return $this->fetch();
    }
 
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function editRel()
    {
        $this->view->engine->layout(false);

        $data['bmdm'] = input('bmdm');
        $data['bmmc'] = input('bmmc');
        $data['zydm'] = input('zydm');  
        $data['zymc'] = input('zymc');  
        $data['dir'] = input('dir');  
        $data['dirName'] = input('dirName');  
        $data['planCode'] = input('planCode'); 
        $data['rxnd'] = input('rxnd'); 
        $data['xsxzdm'] = input('xsxzdm'); 
        
        $this->assign('bmdm', $data['bmdm']);
        $this->assign('bmmc', $data['bmmc']);
        $this->assign('zydm', $data['zydm']);
        $this->assign('zymc', $data['zymc']);
        $this->assign('dir', $data['dir']);
        $this->assign('dirName', $data['dirName']);
        $this->assign('planCode', $data['planCode']);
        $this->assign('rxnd', $data['rxnd']);
        $this->assign('xsxzdm', $data['xsxzdm']);
        
        $bookCommon = new BookCommonasModel();
        $deptList = $bookCommon->getDepts();  // 获取部门列表
        $this->assign('deptList', $deptList);
 
        $viewGetSpecicaltiesModel = new ViewGetSpecicaltiesModel();
        $specialtyList = $viewGetSpecicaltiesModel->where(['bmdm' => $data['bmdm']])->select();
        $this->assign('specialtyList', $specialtyList);

        $dirList = $bookCommon->getDirs();  // 获取方向列表
        $this->assign('dirList', $dirList);

        $rxndCache = new RxndCacheasModel();
        $rxndList = $rxndCache->field('year as id, year as name')->select();
   
        $this->assign('rxndList', $rxndList);

        $planStudentRelModel = new PlanStudentRelModel();
        $relList = $planStudentRelModel->field('bjdm, dir')->where(['plan_code' =>$data['planCode']])->select();

        $this->assign('relList', $relList);

        // 这里需要查出当前筛选条件下且未在已选中清单中的列表
        $viewGetRel = new ViewGetRelasModel();
        $classDirList = $viewGetRel->where(['bmdm' => $data['bmdm'], 'zydm' => $data['zydm']]);
        $this->assign('unRelList', $classDirList);

        return $this->fetch('editRel');
    }

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getUnRelList()
    {
        $result = [
            'status' => false,
            'msg' => '获取失败',
            'data' => []
        ];

        $data['bmdm'] = input('bmdm');
        $data['zydm'] = input('zydm');  
        $data['dir'] = input('dir');  
        $data['rxnd'] = input('rxnd');  
        $data['xsxzdm'] = input('xsxzdm');  
        $data['plan_code'] = input('plan_code');  

        $viewGetRel = new ViewGetRelasModel();

        $where = [];
        if(isset($data['bmdm']) && $data['bmdm'] != "")
        {
            $where[] = ['bmdm', 'eq', $data['bmdm']];
        }
        if(isset($data['zydm']) && $data['zydm'] != "")
        {
            $where[] = ['zydm', 'eq', $data['zydm']];
        }
        if(isset($data['dir']) && $data['dir'] != "")
        {
            $where[] = ['dir', 'eq', $data['dir']];
        }
        if(isset($data['rxnd']) && $data['rxnd'] != "")
        {
            $where[] = ['bjmc', 'like', substr($data['rxnd'], 2, 2).'%'];
        }
        if(isset($data['xsxzdm']) && $data['xsxzdm'] != "")
        {
            $where[] = ['xsxzdm', 'eq', $data['xsxzdm']];
        }

        $classDirList = $viewGetRel->where($where)->select();
        $myClassDirList = $viewGetRel->where(['plan_code'=>$data['plan_code']])->select();
        
        $dataList = [];
        foreach($myClassDirList as $key => $val) {
            if (!array_key_exists($val['bjdm'], $dataList)) {
                $dataList[$val['bjdm']] = $val;
            }
        }

        foreach($classDirList as $key => $val){
            if (!array_key_exists($val['bjdm'], $dataList)) {
                $dataList[$val['bjdm']] = $val;
            }
        }

        $value = [];
        foreach($dataList as $key => $val){ 
            if($val['status'] !=0 && $val['plan_code'] == $data['plan_code']) {
                $value[] = $val['bjdm'].'-'.$val['dir'];
            }
        }

        if($classDirList) {
            $result['status'] = true;
            $result['data'] = $dataList;
            $result['value'] = $value;
        }

        return $result;
    }

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $result = [
            'status' => false,
            'msg' => '保存失败',
            'data' => []
        ];

        $planStudentRelModel = new PlanStudentRelModel();
        return $planStudentRelModel->saveData(input('param.'));
    }

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function studentUnrelList()
    {
        $this->view->engine->layout(false); 
        return $this->fetch('studentUnrelList');
    }

    
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getStudentUnrelList()
    {
        $post = input('param.');
            
        // 没有实体的model暂时这么处理。
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $bjmc = $post['bjmc'];
        
        $page = input('page', 0);
        $index = ($page - 1) * $limit;

        // 调用存储过程
        $model = new Common();
        $list = $model->query("call getStudentUnrelList('".$bjmc."',".$index.",".$limit.");");
        
        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = count($list[1]);
        $re['data'] = $list[0];

        return $re;
    }

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function syncClassList()
    {
        $planStudentRelModel = new PlanStudentRelModel();
        return $planStudentRelModel->syncClassList();
    }

    
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function planUnrelList()
    {
        $this->view->engine->layout(false); 
        return $this->fetch('planUnrelList');
    }

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getPlanUnrelList()
    {
        $post = input('param.');
            
        // $bmdm = "";
        // $zydm = "";
        // $dir = "";

        // 没有实体的model暂时这么处理。
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }
        
        // if(isset($post['bmdm'])){
        //     $bmdm = $post['dep_id'];
        // }
        // if(isset($post['specialty_id'])){
        //     $bmdm = $post['specialty_id'];
        // }
        // if(isset($post['dir'])){
        //     $bmdm = $post['dir'];
        // }

        $page = input('page', 0);
        $index = ($page - 1) * $limit;

        // 调用存储过程
        $model = new Common();
        $list = $model->query("call getPlanUnrelList(".$index.",".$limit.");");
        
        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = count($list[1]);
        $re['data'] = $list[0];

        return $re;
    }
    
}